package com.lianniu.web.pay.action;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.pay.service.YjfService;
import com.lianniu.web.pay.util.PayHttpClient;
import com.lianniu.web.pay.util.YjfConstants;
import com.lianniu.web.pay.util.YjfProperty;
import com.lianniu.web.pay.util.YjfUtil;

@Controller
@RequestMapping("/rest/yjf")
public class YjfRestController {
  private static Logger logger = LoggerFactory.getLogger(YjfRestController.class);
  @Autowired
  YjfService yjfService;
  
  @RequestMapping("/return")
  public String yjfReturn(HttpServletRequest request){
    logger.info("易极付异步返回");
    if(checkSign(request)){
      return "paySuccess";
    }
    return "paySuccess";
  }
  
  @RequestMapping("/notify")
  public @ResponseBody String yjfNotify(HttpServletRequest request){
    logger.info("易极付回调通知start");
    logger.info("接收到的参数为："+JSON.toJSONString(request.getParameterMap()));
    if(!checkSign(request)){
      logger.info("签名校验失败，直接返回");
      return "fail";
    }
    
    Map<String,String> map = new HashMap<>();
    Enumeration<String> enumeration = request.getParameterNames();
    while(enumeration.hasMoreElements()){
      String key =enumeration.nextElement();
      map.put(key, request.getParameter(key));
    }
    yjfService.yjfNotify(map);
    return "success";
  }
  private boolean checkSign(HttpServletRequest request){
    /** 从请求中获取到业务参数*/
    Map<String,String> map = YjfUtil.getParameterMap(request);
    logger.info("返回的参数为："+JSON.toJSONString(map));

    return YjfUtil.checkSign(map, YjfProperty.getSecuritycheckkey());
  }
  @RequestMapping("/dealerRegiste")
  public @ResponseBody ModelMap dealerRegiste(HttpServletRequest request,String userid,ModelMap map){
    Map<String,String> param = new HashMap<String,String>();
    param.put("username",request.getParameter("username"));
    param.put("mobile", request.getParameter("mobile"));
    String url = (String)yjfService.getRequestUrlAndStore(userid, YjfConstants.YJF_DEALER_REGISTER, 
        param).getData();
    
    dealerRegisterCall(url, map);
    return map;
  }
 
  private ModelMap dealerRegisterCall(String url,ModelMap map){
    PayHttpClient client = new PayHttpClient();
    client.setReqContent(url);
    if(!client.call()){
      map.put("result","false");
      map.put("msg","网络出现错误");
      return map;
    }
    String response = client.getResContent();
    logger.info("获取到的返回结果为；"+response);
    JSONObject parsedObject = JSON.parseObject(response);
    if(!"EXECUTE_SUCCESS".equals(parsedObject.get("resultCode"))){
      map.put("result","false");
      map.put("msg", parsedObject.getString("resultMessage"));
      return map;
    }
    
    String qft_userid= parsedObject.getString("userId");
    String orderNo = parsedObject.getString("orderNo");
    Map<String,String> resultMap = new HashMap<>();
    resultMap.put("userId",qft_userid);
    resultMap.put("orderNo",orderNo);
    resultMap.put("service",YjfConstants.YJF_DEALER_REGISTER);
    logger.info("易极付通知");
    BaseResult notifyResult = yjfService.yjfNotify(resultMap);
    logger.info("通知结果为："+JSON.toJSONString(notifyResult));
    
    map.put("result", "true");
    return map;
  }
  @RequestMapping("/supplyRegiste")
  public @ResponseBody BaseResult supplyRegiste(String userid,ModelMap map){
    BaseResult result = yjfService.getRequestUrlAndStore(userid, YjfConstants.YJF_SUPPLY_REGISTER, 
        new HashMap<String,String>());
    logger.info("卖家注册调用结果为："+JSON.toJSONString(result));
    return result;
  }
  
  @RequestMapping("/topay")
  public @ResponseBody BaseResult topay(String userid,String orderids,ModelMap map){
    Map<String,String> param = new HashMap<String,String>();
    param.put("orderid",orderids);
    BaseResult result = yjfService.getRequestUrlAndStore(userid, YjfConstants.YJF_PAY, param);
    logger.info("支付申请："+JSON.toJSONString(result));
    return result;
  }
}
